<?php
/**
 * mysql 异步客户端连接池
 * Created by PhpStorm.
 * User: zhangjincheng
 * Date: 16-7-22
 * Time: 上午10:19
 */

namespace WsdServer\Asyn\AMQP;

use PhpAmqpLib\Connection\AMQPStreamConnection;

use Server\Asyn\AMQP\AMQP;
use Server\Asyn\AsynPool;

class AmqpAsynPool extends AsynPool
{
    const AsynName = 'amqp';

    /**
     * @var array
     */
    private $active;
    private $amqp_channel;

    public function __construct($config, $active)
    {
        parent::__construct($config);
        $this->active = $active;
        $this->client_max_count = $this->config->get('amqp.asyn_max_count', 10);
    }

    /**
     * @return string
     */
    public function getAsynName()
    {
        return self::AsynName . ":" . $this->active;
    }

    /**
     * 获取同步
     * @return Channel
     */
    public function getSync()
    {
        if ($this->amqp_channel!=null) {
            return $this->amqp_channel;
        }
        $this->amqp_channel = $this->connectAMQP($this->active);
        return $this->amqp_channel;
    }

    /**
     * 连接AMQP
     * @param null $active
     */
    protected function connectAMQP($active = null)
    {
        if (!$this->config->has('amqp')) {
            secho("AMQP", "未发现AMQP配置文件");
            while (true) {

            }
            return;
        }
        if (empty($active)) {
            $active = $this->config['amqp']['active'];
        }
        $host = $this->config['amqp'][$active]['host'];
        $port = $this->config['amqp'][$active]['port'];
        $user = $this->config['amqp'][$active]['user'];
        $password = $this->config['amqp'][$active]['password'];
        $vhost = $this->config['amqp'][$active]['vhost'];

        //$connection = new AMQP($host, $port, $user, $password, $vhost);
        $connection = new AMQPStreamConnection($host, $port, $user, $password, $vhost);
        $channel = $connection->channel();
        return $channel;
    }

    function execute($data)
    {
        // TODO: Implement execute() method.
    }

    /**
     * 销毁Client
     * @param $client
     */
    protected function destoryClient($client)
    {
        // TODO: Implement destoryClient() method.
    }
}